package edu.mtu.citizenscience.streamapplication.activity.main;

import edu.mtu.citizenscience.streamapplication.activity.DBHelper;
import edu.mtu.citizenscience.streamapplication.activity.R;
import edu.mtu.citizenscience.streamapplication.activity.R.layout;
import edu.mtu.citizenscience.streamapplication.activity.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Spinner;

public class SCErosionandConditionsActivity extends Activity {
	
	public static final String ROW_ID = "ROW_ID";
	private Long rowID;
	private DBHelper myDbHelper;
	
	private Cursor report;
	
	private CheckBox checkC1;
	private CheckBox checkC2;
	private CheckBox checkC3;
	private CheckBox checkC4;
	private CheckBox checkC5;
	private CheckBox checkC6;
	private EditText otherErosionText;
	private RadioGroup erosionRGroup;
	private EditText erosionCauseText;
	
	private RadioButton selectedRadioE;
	
	String checkboxString1, checkboxString2, checkboxString3, checkboxString4, checkboxString5, checkboxString6, erosionExtentString;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scerosionand_conditions);
		
		myDbHelper = new DBHelper(this);
		myDbHelper.open();
		
		checkC1 = (CheckBox) findViewById(R.id.checkC1);
		checkC2 = (CheckBox) findViewById(R.id.checkC2);
		checkC3 = (CheckBox) findViewById(R.id.checkC3);
		checkC4 = (CheckBox) findViewById(R.id.checkC4);
		checkC5 = (CheckBox) findViewById(R.id.checkC5);
		checkC6 = (CheckBox) findViewById(R.id.checkC6);
		otherErosionText = (EditText) findViewById(R.id.otherErosionText);
		erosionRGroup = (RadioGroup) findViewById(R.id.erosionRGroup);
		erosionCauseText = (EditText) findViewById(R.id.erosionCauseText);
		
			
		rowID = this.getIntent().getLongExtra(this.ROW_ID, -1);
		populateFields();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.scerosionand_conditions, menu);
		return true;
	}
	
	private void populateFields() {
    	report = myDbHelper.fetchReport(rowID, "Erosion_Desc");
    	
        if (report.getCount() == 1) {
        	
        	if(report.getString(report.getColumnIndexOrThrow("erosion_condition_one")).equals("true"))
            	checkC1.setChecked(true);
            else
            	checkC1.setChecked(false);
        	
        	if(report.getString(report.getColumnIndexOrThrow("erosion_condition_two")).equals("true"))
            	checkC2.setChecked(true);
            else
            	checkC2.setChecked(false);
        	
        	if(report.getString(report.getColumnIndexOrThrow("erosion_condition_three")).equals("true"))
            	checkC3.setChecked(true);
            else
            	checkC3.setChecked(false);
        	
        	if(report.getString(report.getColumnIndexOrThrow("erosion_condition_four")).equals("true"))
            	checkC4.setChecked(true);
            else
            	checkC4.setChecked(false);
        	
        	if(report.getString(report.getColumnIndexOrThrow("erosion_condition_five")).equals("true"))
            	checkC5.setChecked(true);
            else
            	checkC5.setChecked(false);
        	
        	if(report.getString(report.getColumnIndexOrThrow("erosion_condition_six")).equals("true"))
            	checkC6.setChecked(true);
            else
            	checkC6.setChecked(false);
        	
        	otherErosionText.setText(report.getString(report.getColumnIndexOrThrow("erosion_condition_other")));
            
        	//compare conditionRadios to string in database and select matching
            erosionExtentString = report.getString(report.getColumnIndexOrThrow("erosion_extent")); 
            if(erosionExtentString.equals("Minor"))
            	erosionRGroup.check(R.id.radio_erosionminor);
            else if(erosionExtentString.equals("Moderate"))
            	erosionRGroup.check(R.id.radio_erosionmoderate);
            else if(erosionExtentString.equals("Extreme"))
            	erosionRGroup.check(R.id.radio_erosionextreme);
            else
            	erosionRGroup.check(-1);
            
            erosionCauseText.setText(report.getString(report.getColumnIndexOrThrow("erosion_cause")));
          
        }
        else
        {
        	erosionRGroup.check(-1);
        }
    }
    
    private void saveLocation(){
    	
    	String selectedRString;
    	
    	if(checkC1.isChecked())
			checkboxString1 = "true";
		else
			checkboxString1 = "false";
    	
    	if(checkC2.isChecked())
			checkboxString2 = "true";
		else
			checkboxString2 = "false";
    	
    	if(checkC3.isChecked())
			checkboxString3 = "true";
		else
			checkboxString3 = "false";
    	
    	if(checkC4.isChecked())
			checkboxString4 = "true";
		else
			checkboxString4 = "false";
    	
    	if(checkC5.isChecked())
			checkboxString5 = "true";
		else
			checkboxString5 = "false";
    	
    	if(checkC6.isChecked())
			checkboxString6 = "true";
		else
			checkboxString6 = "false";

		if((otherErosionText.getText().toString()).length()==0)
			otherErosionText.setText("None");
		if((erosionCauseText.getText().toString()).length()==0)
			erosionCauseText.setText("Unknown");
		
		selectedRadioE = (RadioButton) findViewById(erosionRGroup.getCheckedRadioButtonId());
		
		if(erosionRGroup.getCheckedRadioButtonId() > 0)
			selectedRString=selectedRadioE.getText().toString();
		else
			selectedRString="Empty";
		
		if(report.getCount() == 0){
			
			myDbHelper.createErosion(rowID, checkboxString1, checkboxString2, checkboxString3, checkboxString4, checkboxString5, checkboxString6, 
					otherErosionText.getText().toString(), selectedRString, erosionCauseText.getText().toString());
		}
		else{
			myDbHelper.updateErosion(rowID, checkboxString1, checkboxString2, checkboxString3, checkboxString4, checkboxString5, checkboxString6, 
					otherErosionText.getText().toString(), selectedRString, erosionCauseText.getText().toString());
		}
    }
    
	
	/** Called when the user clicks the Continue button */
	public void cont(View view) {
		saveLocation();
		myDbHelper.close();
	    
		this.finish();
	}
	
	public void cancel(View view) {
		myDbHelper.close();
	    
		this.finish();
	}

}
